package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tudou.itemcodecrypt.CryptException;
import com.tudou.itemcodecrypt.ItemCodeCrypt;
import common.Constant;

import model.Item;

import util.KeyGen;
import util.MyJson;
import util.ParameterCheck;
import util.PinYin;

import dao.ItemDao;

/**
 * Servlet implementation class CreateItem
 */
public class CreateItem extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateItem() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		String name = ParameterCheck
				.stringDefault(request.getParameter("name"));
		String tags = ParameterCheck
				.stringDefault(request.getParameter("tags"));
		String name_py = PinYin.getPinyin(name);
		String name_py2 = PinYin.getPinyin2(name_py);
		String imgpath = ParameterCheck
		.stringDefault(request.getParameter("imgpath"));
		String key = "";
		Cookie[] cookies = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			if ("user".equals(cookies[i].getName()))
				key = cookies[i].getValue();
		}
		if("".equals(imgpath))
			imgpath="img/default.jpg";

		if (!KeyGen.cookieValid(key)) {
			pw.write(MyJson.toJson("msg", "-1"));
		} 
		else if ("".equals(name)) {
				pw.write(MyJson.toJson("msg", "名字不能为空"));
			} 
		else if ("".equals(tags)) {
				pw.write(MyJson.toJson("msg", "标签不能为空"));
			} 
		else {
				Item item = new Item();
				item.setName(name);
				item.setName_py(name_py);
				item.setName_py2(name_py2);
				item.setTags(tags);
				item.setUid(Integer.valueOf( KeyGen.getId(key)));
				item.setImg(imgpath.replace(Constant.imgurl, ""));
				ItemDao itemdao = new ItemDao();
				int n = itemdao.CreateItem(item);
				String code="";
				if (n <= 0) {
					pw.write(MyJson.toJson("msg", "系统问题，暂时不能创建新人，请稍后再试，抱歉！"));
				} else {
					try {
						code= ItemCodeCrypt.getCode(n);
						pw.write(MyJson.toJson("succ" ,code));
					} catch (CryptException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		pw.flush();
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
